// miniprogram/pages/PsUser/PsUser.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '男',
    checked: true,
    checked3: true,
    checked4:false,
    show: false,
    show2:false,
    show3: false,
    text:'',
    option1: [
      { text: '请选择', value: 0 },
      { text: '正常', value: 1 },
      { text: '冻结', value: 2 }
    ],
    qy: 0,
    option2: [
      { text: '请选择', value: '请选择' }
    ],
    value2: '请选择'
  },
  onChangex(event) {
    this.setData({
      radio: event.detail
    });
    console.log('DNF',this.data.radio)
  },
  vv(){
    this.setData({ show3: true });
  },
  ins(e11){
    console.log('userName', this.data.username2)
    console.log('roleId', this.data.hh)
    console.log('userStatus', this.data.checked4)
    console.log('sex', this.data.radio)
    if (this.data.hh == null){
      wx.showToast({
        title: '所属角色',
      })
      return
    }

    if (this.data.checked4 == false) {
      this.data.checked4 = 1
    } else if (this.data.checked4 == true) {
      this.data.checked4 = 2
    }

    wx.request({
      url: getApp().appData.site+'/accp/psUser/ins',
      data: {
        userName: this.data.username2,
        openid: this.data.username3,
        psRole: this.data.hh,
        userStatus: this.data.checked4,
        sex: this.data.radio,
        email:this.data.username4
        
      },
      success: (res) => {
        console.log(res)
        wx.request({
          url: getApp().appData.site+'/accp/psUser/selectAll1',
          success: (res) => {
            console.log(res)
            console.log(res.data)
            this.setData({
              aa: res.data
            })
            console.log('aa', this.data.aa)
          }
        })
        this.setData({ show3: false });
      }
    })

  },
  upd(e9){
    console.log('id',this.data.dataInfo)
    console.log('userName', this.data.username)
    console.log('roleId', this.data.hh)
    console.log('userStatus', this.data.checked3)
    console.log('openid', this.data.openid)
    console.log('email', this.data.email)
    console.log('radio', this.data.radio)
    if (this.data.checked3 == false) {
      this.data.checked3 = 1
    } else if (this.data.checked3 == true) {
      this.data.checked3 = 2
    }
    wx.request({
      url: getApp().appData.site+'/accp/psUser/updateid',
      data: {
        // obj: JSON.stringify(obj)
        id: this.data.dataInfo,
        userName: this.data.username,
        psRole: this.data.hh,
        userStatus: this.data.checked3,
        openid: this.data.openid,
        email: this.data.email,
        sex: this.data.radio
      },
      success: (res) => {
        console.log(res)
        wx.request({
          url: getApp().appData.site+'/accp/psUser/selectAll1',
          success: (res) => {
            console.log(res)
            console.log(res.data)
            this.setData({
              aa: res.data
            })
            console.log('aa', this.data.aa)
          }
        })
        this.setData({ show2: false });
      }
    })

  },
  item(e8){
    console.log(e8)
    this.setData({
      hh:e8.detail
    })
    console.log(this.data.hh)
  },
  onClickIcon(e7){
    this.setData({
      username:e7.detail.value
    })
    console.log(this.data.username)
  },
  onClickIcon2(e10){
    this.setData({
      username2: e10.detail.value
    })
    console.log(this.data.username2)
  },
  onClickIcon3(e12){
    this.setData({
      username3: e12.detail.value,
      openid: e12.detail.value
    })
    console.log(this.data.username3)
  },
  onClickIcon4(e13) {
    this.setData({
      username4: e13.detail.value,
      email: e13.detail.value
    })
    console.log(this.data.username4)
  },
  onChange3({ detail }) {
    wx.showModal({
      title: '提示',
      content: '是否切换开关？',
      success: res => {
        if (res.confirm) {
          this.setData({ checked3: detail });
        }
        console.log(this.data.checked3)
      }
    });
  },
  onChange4({ detail }) {
    wx.showModal({
      title: '提示',
      content: '是否切换开关？',
      success: res => {
        if (res.confirm) {
          this.setData({ checked4: detail });
        }
        console.log(this.data.checked4)
      }
    });
  },
  onChange2({ detail }) {
    wx.showModal({
      title: '提示',
      content: '是否切换开关？',
      success: res => {
        if (res.confirm) {
          this.setData({ checked: detail });
          console.log(this.data.checked)
        }
        if (this.data.checked==false){
          this.data.checked = 1
        } else if (this.data.checked == true){
          this.data.checked = 2
        }
        wx.request({
          url: getApp().appData.site+'/accp/psUser/updateUserStatus',
          data: {
            id: this.data.dataInfo,
            userStatus: this.data.checked
          },
          success: (res) => {
            wx.request({
              url: getApp().appData.site+'/accp/psUser/selectAll1',
              success: (res) => {
                console.log(res)
                console.log(res.data)
                this.setData({
                  aa: res.data
                })
                console.log('aa', this.data.aa)
              }
            })
          }
        })
      }
    });
  },
  showPopup(e3) {
    this.setData({ show: true });
    const dataInfo = e3.currentTarget.dataset.info;
    this.setData({
      dataInfo : e3.currentTarget.dataset.info
    })
    wx.request({
      url: getApp().appData.site+'/accp/psUser/selectid',
      data:{
        id:this.data.dataInfo
      },
      success: (res) => {
        console.log("xx",res)
        this.setData({
          checked:res.data.userStatusStr2
        })
        console.log("xx", this.data.checked)
      }
    })

  },
  showPopup2(e6) {
    this.setData({ show2: true });
    const dataInfo = e6.currentTarget.dataset.info;
    this.setData({
      dataInfo: e6.currentTarget.dataset.info
    })
    console.log(this.data.dataInfo)

    wx.request({
      url: getApp().appData.site+'/accp/psUser/selectid',
      data: {
        id: this.data.dataInfo
      },
      success: (res3) => {
        console.log("ff", res3)
        this.setData({
          checked3: res3.data.userStatusStr2,
          username: res3.data.userName,
          value2: res3.data.psRole.id,
          hh:res3.data.psRole.id,
          openid:res3.data.openid,
          email:res3.data.email,
          radio:res3.data.sex
        })
        console.log("xx2", this.data.username)
        console.log("xx3", this.data.value2)
        console.log("xx", this.data.checked3)
        
      }
    })


  },
  
  onClose2(){
    this.setData({ show2: false });
    this.setData({ show3: false });
  },
  onClose() {
    this.setData({ show: false });
  },
  cx(e4){
    console.log('001',this.data.text)
    console.log('001',this.data.qy)
    wx.request({
      url: getApp().appData.site+'/accp/psUser/selectAll2',
      data:{
        userName:this.data.text,
        userStatus:this.data.qy
      },
      success: (res) => {
        console.log(res)
        this.setData({
          aa: res.data
        })
        console.log('aa', this.data.aa)
      }
    })
  },
  xx(e3){
    const dataInfo = e3.currentTarget.dataset.info;
    console.log(dataInfo)
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: getApp().appData.site+'/accp/psUser/del',
            data: {
              id: dataInfo
            },
            success: (res) => {
              console.log('删除成功')
              wx.redirectTo({
                
                url: '../PsUser/PsUser',

              })
            },
            fail: (ex) => {
              wx.showToast({
                title: '主外键问题',
              })
              console.log('主外键问题');
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  so(e2) {
    this.data.qy = e2.detail;
    console.log(22, e2.detail);
  },
  onChange(event) {
    // event.detail 为当前输入的值
    this.data.text = event.detail.value;
    console.log(11, event.detail.value);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: getApp().appData.site+'/accp/psUser/selectAll1',
      success: (res) => {
        console.log(res)
        console.log(res.data)
        this.setData({
          aa: res.data
        })
        console.log('aa', this.data.aa)
      }
    })

    wx.request({
      url: getApp().appData.site+'/accp/psUser/selectAll3',
      success: (res) => {
        console.log(res)
        console.log(res.data)
        this.setData({
          xx: res.data
        })
        console.log('aa', this.data.aa)
      }
    })

    wx.request({
      url: getApp().appData.site+'/accp/psRole/ByroleNameandisStart',
      success: (res2) => {
        console.log(res2)
        console.log(res2.data)
        var myfor = [];
        for(var i in res2.data){
          myfor.push({
            text: res2.data[i].roleName,
            value: res2.data[i].id
          })
        }
        this.setData({
          ss: res2.data,
          option2:myfor
        })
        console.log('option2', this.data.option2)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})